package com.itfly.service;

import com.itfly.entity.Product;
import com.itfly.exception.BusinessException;
import com.itfly.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//商品业务逻辑组件
@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;
    //新增商品
    public void add(Product product){
        //判断

        Product one= productMapper.selectByPno(product.getPno());
        if(one!=null){
            throw new BusinessException("商品编号已存在，请重新输入");
        }
        productMapper.insertOne(product);
    }
    //查询商品
    public List<Product> search(){
        List<Product> products = productMapper.selectAll();
        return products;
    }
    //删除
    public  void delete(String pno){
        productMapper.deleteByPno(pno);
    }
    //根据pno查询
    public Product searchByPno(String pno){
        return productMapper.selectByPno(pno);
    }

    public void update(Product product) {
        productMapper.updateAll(product);
    }
}